//轨迹坐标数组
var pppts = [100];

var points = [100];

//开关门时间
var startOpenTime = "";
var startCloseTime = "";
var endOpenTime = "";
var endCloseTime = "";

var startPlus = "";
var endPlus = "";

var myP1 = new BMap.Point(116.380967,39.913285);    //起点
var myP2 = new BMap.Point(116.424374,39.914668);    //终点

//起始点标注
var label1 = "";
var label2 = "";

var label3 = "";
var label4 = "";

var label5 = "";
var label6 = "";

var label7 = "";
var label8 = "";


var sLat = "";
var sLng = "";

var eLat = "";
var eLng = "";

var carNumber = "";

//终点坐标
var endPoint1 = "";
var endPoint2 = "";

$(function () {

    var theHeight = $('.height40').height();
    $('.tbody-div').height(theHeight-110+'px');

    //通过场站id，异步获取车辆信息
    $('.b-botom').click(function () {
        var stationId = $(this).val();
        //ajax start
        $.ajax({
            type: "GET",
            url: "../car/getCarsByStationId",
            dataType : 'json',
            data:{
                stationId:stationId
            },
            success: function(data){
                var str = "";
                $.each(data,function(index, n){
                    // var carNo = data[index].carNumber.replace('010','京');
                    // str += "<li class='p-bt-10 car' value='"+carNo+"'>"+carNo+"</li>"
                    str += "<li class='p-bt-10 car' value='"+data[index].carNumber+"' name='"+data[index].carNumber+"'>"+data[index].carCnumber+"</li>"
                });
                $('.carList').html(str);
            }
        });
        //ajax end
    });


    //获取每辆车的任务记录
    $(document).on("click",".car",function(){
        carNumber = $(this).attr("name");
        var zhouQi = $(".twin-bg").html();
        if(zhouQi == "当周")
        {
            zhouQi = "zhou";
        }
        else
        {
            zhouQi = "yue";
        }

        // carNumber = carNumber.replace('京','010');
        // alert(carNumber);
        //通过车牌号查询任务记录
        //ajax start
        $.ajax({
            type: "GET",
            url: "../rate/getMessionByCarNumber",
            dataType : 'json',
            data:{
                carNumber:carNumber,
                zhouQi:zhouQi
            },
            success: function(data){
                /*var str = "<thead class='font-bold'>";
                str += "<td>任务单编号</td><td>始发地</td><td>目的地</td><td>装载率</td>";
                str += "<td>货物体积</td><td>装车用时</td><td>卸车用时</td>";
                str += "</thead>";*/

                var str = "";
                // str+="<div class='tbody-div' style='height: 100px;overflow-y: scroll;display: block;width: 100%;flex-grow: 1;'><table class='table table-striped'> <tbody style='width: 100%;'>";

                $.each(data,function(index, n){
                    // alert(data[index].rateValue);
                    if(data[index].rateValue == "0.0")
                    {

                    }
                    else
                    {
                        var ontime = data[index].onTime.replace("0天0小时","");
                        var offtime = data[index].ofTime.replace("0天0小时","");

                        var ontime = ontime.replace("0天","");
                        var offtime = offtime.replace("0天","");

                        var ontime = ontime.replace("0小时","");
                        var offtime = offtime.replace("0小时","");

                        // ontime = ontime.replace("分","'");
                        // ontime = ontime.replace("秒","''");
                        // ontime = ontime.replace("小时","''");
                        //
                        // offtime = offtime.replace("分","'");
                        // offtime = offtime.replace("秒","''");
                        var mNumber = data[index].messionsNumber;
                        // alert(mNumber);
                        str += "<tr class='messionId' name='"+data[index].messionsNumber+"'>";
                        str += "<td >"+data[index].messionsNumber+"</td><td>"+data[index].startPlace+"</td><td>"+data[index].endPlace+"</td>";
                        str += "<td onclick='clicktd("+data[index].messionId+")'><a href='javascript:void(0);'>"+data[index].rateValue+"%</a></td><td>"+data[index].tiji+"m<sup>3</sup></td><td>"+ontime+"</td><td>"+offtime+"</td>";
                        str += "</tr>";
                    }
                });
                $('#messionString').html(str);
            }
        });
        //ajax end
    });

    //获取每辆车的任务记录
    $(document).on("click",".t-c",function(){
        var zhouQi = $(".twin-bg").html();
        if(zhouQi == "当周")
        {
            zhouQi = "zhou";
        }
        else
        {
            zhouQi = "yue";
        }

        // carNumber = carNumber.replace('京','010');
        // alert(carNumber);
        //通过车牌号查询任务记录
        //ajax start
        $.ajax({
            type: "GET",
            url: "../rate/getMessionByCarNumber",
            dataType : 'json',
            data:{
                carNumber:carNumber,
                zhouQi:zhouQi
            },
            success: function(data){
                /*var str = "<thead class='font-bold'>";
                str += "<td>任务单编号</td><td>始发地</td><td>目的地</td><td>装载率</td>";
                str += "<td>货物体积</td><td>装车用时</td><td>卸车用时</td>";
                str += "</thead>";*/
                var str = "";

                $.each(data,function(index, n){
                    if(data[index].rateValue == "0.0")
                    {

                    }
                    else
                    {
                        var ontime = data[index].onTime.replace("0天0小时","");
                        var offtime = data[index].ofTime.replace("0天0小时","");

                        var ontime = ontime.replace("0天","");
                        var offtime = offtime.replace("0天","");

                        var ontime = ontime.replace("0小时","");
                        var offtime = offtime.replace("0小时","");

                        // ontime = ontime.replace("分","'");
                        // ontime = ontime.replace("秒","''");
                        // ontime = ontime.replace("小时","''");
                        //
                        // offtime = offtime.replace("分","'");
                        // offtime = offtime.replace("秒","''");
                        var mNumber = data[index].messionsNumber;
                        // alert(mNumber);
                        str += "<tr class='messionId' name='"+data[index].messionsNumber+"'>";
                        str += "<td >"+data[index].messionsNumber+"</td><td>"+data[index].startPlace+"</td><td>"+data[index].endPlace+"</td>";
                        str += "<td onclick='clicktd("+data[index].messionId+")'><a href='javascript:void(0);'>"+data[index].rateValue+"%</a></td><td>"+data[index].tiji+"m<sup>3</sup></td><td>"+ontime+"</td><td>"+offtime+"</td>";
                        str += "</tr>";
                    }

                });
                $('#messionString').html(str);
            }
        });
        //ajax end
    });




    //通过任务单号查询坐标点绘制地图
    $(document).on("click",".messionId",function(){
        var messionId = $(this).attr('name');
        // alert(messionId);
        //ajax start
        $.ajax({
            type: "GET",
            url: "../rate/getMessionPotntByMessionId",
            dataType : 'json',
            data:{
                messionId:messionId
            },
            success: function(data){
                var datalength = data.length;
                var i = 0;
                $.each(data,function(index, n){
                    i++;
                    var lat = data[index].lat;
                    // alert(lat);
                    var lng = data[index].lng;

                    // alert(lng);
                    var pt = new Object();
                    pt.lat = lat;
                    pt.lng = lng;

                   if(i == 1)
                   {
                       endPoint1 = lat;
                       endPoint2 = lng;
                   }

                    points.push(new BMap.Point(lat, lng));
                });

                sLat = data[0].lat;
                sLng = data[0].lng;

                eLat = data[datalength-1].lat;
                eLng = data[datalength-1].lng;
                init();
            }
        });
        //ajax end


        //ajax start
        $.ajax({
            type: "GET",
            url: "../rate/getMessionOpenByMessionNumber",
            dataType : 'json',
            data:{
                messionId:messionId
            },
            success: function(data){
                // alert(data.startOpenTime);

                startOpenTime = data.startOpenTime
                startCloseTime = data.startCloseTime
                endOpenTime = data.endOpenTime
                endCloseTime = data.endCloseTime

                startPlus = data.startPlus
                endPlus = data.endPlus
            }
        });
        //ajax end

    });

    //每隔3秒定时请求更新地图
    // window.setInterval("changeMapCar()",3000);
});


//获取所有点的坐标


var map; //百度地图对象
var car; //汽车图标
var label; //信息标签
var centerPoint;

var timer; //定时器
var index = 0; //记录播放到第几个point

var followChk, playBtn, pauseBtn, resetBtn; //几个控制按钮

function init() {
    followChk = document.getElementById("follow");
    playBtn = document.getElementById("play");
    pauseBtn = document.getElementById("pause");
    resetBtn = document.getElementById("reset");

//初始化地图,选取第一个点为起始点
    map = new BMap.Map("container");
    map.centerAndZoom(points[1], 14);
    map.enableScrollWheelZoom();
    // map.addControl(new BMap.NavigationControl());
    map.addControl(new BMap.ScaleControl());
    // map.addControl(new BMap.OverviewMapControl({isOpen: true}));
    var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角，仅包含平移和缩放按钮
    map.addControl(top_right_navigation);

//通过DrivingRoute获取一条路线的point
    var driving = new BMap.DrivingRoute(map);
    driving.search(new BMap.Point(sLat, sLng), new BMap.Point(eLat, eLng));
    driving.setSearchCompleteCallback(function() {
//得到路线上的所有point
        alert(points[points.length-1].lng);
        alert(points[points.length-1].lat);
        points = driving.getResults().getPlan(0).getRoute(0).getPath();
        alert(points[points.length-1].lng);
        alert(points[points.length-1].lat);
//画面移动到起点和终点的中间
        centerPoint = new BMap.Point((points[0].lng + points[points.length - 1].lng) / 2, (points[0].lat + points[points.length - 1].lat) / 2);
        map.panTo(centerPoint);
//连接所有点
        map.addOverlay(new BMap.Polyline(points, {strokeColor: "green", strokeWeight: 5, strokeOpacity: 1}));

//显示小车子
        label = new BMap.Label("", {offset: new BMap.Size(-20, -20)});
        car = new BMap.Marker(points[0]);
        car.setLabel(label);
        // map.addOverlay(car);


        //显示文本标注
        var point1 = new BMap.Point(points[1].lng,points[1].lat);
        var opts1 = {
            position : point1,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(30, -100)    //设置文本偏移量
        }
        label1 = new BMap.Label(endOpenTime, opts1);  // 创建文本标注对象
        label1.setStyle({
            color : "white",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"125px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            // background:"url(http://60.205.182.120:8080/car_networking/static/images/open.png) no-repeat",
            background:"url(http://60.205.182.120:8080/car_networking/static/images/open.png) no-repeat",
            cursor:"pointer"
        });
        // map.addOverlay(label1);

        var point2 = new BMap.Point(points[1].lng,points[1].lat);
        var opts2 = {
            position : point2,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(30, -50)    //设置文本偏移量
        }
        label2 = new BMap.Label(endCloseTime, opts2);  // 创建文本标注对象
        label2.setStyle({
            color : "white",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"125px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            background:"url(http://60.205.182.120:8080/car_networking/static/images/close.png) no-repeat",
            cursor:"pointer"
        });
        // map.addOverlay(label2);



        //显示文本标注
        var point3 = new BMap.Point(points[points.length-1].lng,points[points.length-1].lat);
        var opts3 = {
            position : point3,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(30, -100)    //设置文本偏移量
        }
        label3 = new BMap.Label(startOpenTime, opts3);  // 创建文本标注对象
        label3.setStyle({
            color : "white",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"125px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            background:"url(http://60.205.182.120:8080/car_networking/static/images/open.png) no-repeat",
            cursor:"pointer"
        });
        // map.addOverlay(label3);

        var point4 = new BMap.Point(points[points.length-1].lng,points[points.length-1].lat);
        var opts4 = {
            position : point4,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(30, -50)    //设置文本偏移量
        }
        label4 = new BMap.Label(startCloseTime, opts4);  // 创建文本标注对象
        label4.setStyle({
            color : "white",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"125px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            background:"url(http://60.205.182.120:8080/car_networking/static/images/close.png) no-repeat",
            cursor:"pointer"
        });
        // map.addOverlay(label4);

        var point7 = new BMap.Point(points[points.length-1].lng,points[points.length-1].lat);
        var opts7 = {
            position : point7,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(160, -25)    //设置文本偏移量
        }
        label7 = new BMap.Label(endPlus, opts7);  // 创建文本标注对象
        label7.setStyle({
            color : "#000000",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"20px",                //高度
            width:"55px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"23px",            //行高，文字垂直居中显示
            border:"1px solid",
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            // background:"url(http://60.205.182.120:8080/car_networking/static/images/close.png) no-repeat",
            cursor:"pointer"
        });
        map.addOverlay(label7);


        //起点终点
      /*  alert(endPoint1);
        alert(endPoint2);*/
        var point5 = new BMap.Point(endPoint1,endPoint2);
        // alert(point5);
        var opts5 = {
            position : point5,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(-60, -40)    //设置文本偏移量
        }
        label5 = new BMap.Label("", opts5);  // 创建文本标注对象
        label5.setStyle({
            color : "red",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"120px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            background:"url(http://60.205.182.120:8080/car_networking/static/images/end.png) no-repeat",
            cursor:"pointer"
        });
        map.addOverlay(label5);

        var point6 = new BMap.Point(points[points.length-1].lng,points[points.length-1].lat);
        var opts6 = {
            position : point6,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(-10, -20)    //设置文本偏移量
        }
        label6 = new BMap.Label("", opts6);  // 创建文本标注对象
        label6.setStyle({
            color : "red",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"120px",                //高度
            width:"125px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"120px",            //行高，文字垂直居中显示
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            background:"url(http://60.205.182.120:8080/car_networking/static/images/start.png) no-repeat",
            cursor:"pointer"
        });
        map.addOverlay(label6);

        var point8 = new BMap.Point(points[1].lng,points[1].lat);
        var opts8 = {
            position : point8,    // 指定文本标注所在的地理位置
            offset   : new BMap.Size(160, -25)    //设置文本偏移量
        }
        label8 = new BMap.Label(startPlus, opts8);  // 创建文本标注对象
        label8.setStyle({
            color : "#000000",
            // fontSize : "12px",
            // height : "100px",
            // lineHeight : "20px",
            fontFamily:"微软雅黑",
            fontSize:"14px",               //字号
            border:"0",                    //边
            height:"20px",                //高度
            width:"55px",                 //宽
            textAlign:"center",            //文字水平居中显示
            lineHeight:"23px",            //行高，文字垂直居中显示
            border:"1px solid",
            // background:"url(http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/actions/gohome.png)",    //背景图片，这是房产标注的关键！
            // background:"url(http://60.205.182.120:8080/car_networking/static/images/close.png) no-repeat",
            cursor:"pointer"
        });
        map.addOverlay(label8);

//点亮操作按钮
        /*playBtn.disabled = false;
        resetBtn.disabled = false;*/
    });
}

function play() {
    playBtn.disabled = true;
    pauseBtn.disabled = false;

    var point = points[index];
    if(index > 0) {
        map.addOverlay(new BMap.Polyline([points[index - 1], point], {strokeColor: "red", strokeWeight: 1, strokeOpacity: 1}));
    }
    label.setContent("经度: " + point.lng + "<br>纬度: " + point.lat);
    car.setPosition(point);
    index++;
    if(followChk.checked) {
        map.panTo(point);
    }
    if(index < points.length) {
        timer = window.setTimeout("play(" + index + ")", 200);
    } else {
        playBtn.disabled = true;
        pauseBtn.disabled = true;
        map.panTo(point);
    }
}

function pause() {
    playBtn.disabled = false;
    pauseBtn.disabled = true;

    if(timer) {
        window.clearTimeout(timer);
    }
}

function reset() {
    followChk.checked = false;
    playBtn.disabled = false;
    pauseBtn.disabled = true;

    if(timer) {
        window.clearTimeout(timer);
    }
    index = 0;
    car.setPosition(points[0]);
    map.panTo(centerPoint);
}



//获取所有点的坐标
// 百度地图API功能
// var map = new BMap.Map("allmap");
// map.centerAndZoom(new BMap.Point(sLat, sLng), 12);
//
// // var myP1 = new BMap.Point(116.380967,39.913285);    //起点
// // var myP2 = new BMap.Point(116.424374,39.914668);    //终点
// var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/Mario.png", new BMap.Size(32, 70), {    //小车图片
//     //offset: new BMap.Size(0, -5),    //相当于CSS精灵
//     imageOffset: new BMap.Size(0, 0)    //图片的偏移量。为了是图片底部中心对准坐标点。
// });
// var driving2 = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});    //驾车实例
// driving2.search(myP1, myP2);    //显示一条公交线路
//
// function runMap() {
//
//     var driving = new BMap.DrivingRoute(map);    //驾车实例
//     driving.search(myP1, myP2);
//     driving.setSearchCompleteCallback(function(){
//         var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
//         var pts = pppts;
//         console.log(pts);
//         var paths = pts.length;    //获得有几个点
//
//         var carMk = new BMap.Marker(pts[0],{icon:myIcon});
//         map.addOverlay(carMk);
//         i=0;
//         function resetMkPoint(i){
//             carMk.setPosition(pts[i]);
//             if(i < paths){
//                 setTimeout(function(){
//                     i++;
//                     resetMkPoint(i);
//                 },100);
//             }
//         }
//         setTimeout(function(){
//             resetMkPoint(5);
//         },100)
//
//     });
// }
//
// setTimeout(function(){
//     runMap();
// },1500);